package com.yanfan.ibms.common.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * http请求工具类
 */
public class HttpUtil {


    /**
     * 发送HttpPost请求，参数为map
     * @param r 请求URL地址
     * @param headers  请求头参数
     * @param body  请求参数
     * @return
     */
    public static JSONObject sendPost(String r, Map<String, String> headers, Map<String, Object> body) {
        StringBuilder response = new StringBuilder();
        URL url = null;
        HttpURLConnection connection = null;
        JSONObject res=new JSONObject();
        try {
            url = new URL(r);
            connection = (HttpURLConnection) url.openConnection();

            // 设置请求方法（GET、POST等，根据你的需求设置）
            connection.setRequestMethod("POST");
            //设置请求头
            for (Map.Entry<String, String> e : headers.entrySet()) {
                connection.setRequestProperty(e.getKey(),e.getValue());
            }
            // 设置请求数据类型
            connection.setRequestProperty("Content                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              -Type", "application/json-patch+json,application/json,text/json,application/*+json");
//            connection.setRequestProperty("Content-Type", "application/json");
            // 设置响应数据类型
//            connection.setRequestProperty("Accept", "text/plain,application/json,text/json");
            connection.setRequestProperty("Accept", "application/json");
            connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");

            connection.setDoOutput(true);
            connection.setDoInput(true);
            OutputStream outputStream = connection.getOutputStream();

            //设置请求参数
//            for (Map.Entry<String, String> e : querys.entrySet()) {
//
//            }
            JSONObject jsonObject = new JSONObject(body);//  todo   JSONObject jsonObject = new JSONObject(!body.isEmpt());
            String s = jsonObject.toString();

//            String jsonParameters = "{\"loginName\":\"user\",\"password\":123456,\"verifyCode\":\""+formattedDate+"\"}";
            outputStream.write(s.getBytes(StandardCharsets.UTF_8));
            outputStream.flush();
            outputStream.close();

            // 获取响应数据
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));

            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();

            // 输出响应数据
            //res = new JSONObject(JSON.parseObject(response.toString())); //todo  res = new JSONObject(Boolean.parseBoolean(response.toString()));
            res = JSON.parseObject(response.toString());

            // 关闭连接
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }




        return res;
    }



}
